using Circus.CodeEditor;
using Circus.CodeEditor.TextSource;
using Circus.Syntax;
using Circus.Syntax.Lexer;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Circus.CodeEditor {
    public class EditSyntaxPaint : SyntaxPaint {
        private CodeEditor owner;
        private WhiteSpace whiteSpace;
        private Selection2 selection;
        private EditLineStyles lineStyles;
        private LineSeparator lineSeparator;
        private Braces braces;
        private Outlining outlining;
        private EditHyperText hyperText;
        private EditSpelling spelling;
        private Gutter gutter;
        private DisplayStrings displayLines;
        private LexStyle errorStyle;
        private LexStyle snippetStyle;
        private IList<ILineStyle> internalLineStyles;
        private Color selBackColor;
        private Color selForeColor;
        private bool drawSelection;
        private bool needPaint;
        private Color readonlyForeColor = Color.Empty;
        private Color readonlyBackColor = Color.Empty;
        private Color disabledForeColor = Color.Empty;
        private Color disabledBackColor = Color.Empty;
        private Color columnsIndentForeColor = EditConsts.DefaultColumnsIndentForeColor;
        private bool drawColumnsIndent = true;
        private bool syntaxErrorsHints = true;
        private bool inHighlightOutlineArea = false;
        private bool inHighlightBlockArea = false;
        private OutlineRange hightlightOutlineRange;
        private Range hightlightBlockRange;
        private Hashtable syntaxErrors;
        private bool inPrinting;
      
        [Browsable(false), Description("Gets or sets object that can make lexical analysis for the control's content."), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override Circus.Syntax.Lexer.Lexer Lexer {
            get {
                return this.owner.Lexer;
            }
        }
        [Description("Gets or sets foreground color used in the readonly state.")]
        public virtual Color ReadonlyForeColor {
            get {
                return this.readonlyForeColor;
            }
            set {
                if (this.readonlyForeColor != value) {
                    this.readonlyForeColor = value;
                    this.OnReadonlyForeColorChanged();
                }
            }
        }
        [Description("Gets or sets background color used in the readonly state.")]
        public virtual Color ReadonlyBackColor {
            get {
                return this.readonlyBackColor;
            }
            set {
                if (this.readonlyBackColor != value) {
                    this.readonlyBackColor = value;
                    this.OnReadonlyBackColorChanged();
                }
            }
        }
        [Description("Gets or sets foreground color used in the disabled state.")]
        public virtual Color DisabledForeColor {
            get {
                return this.disabledForeColor;
            }
            set {
                if (this.disabledForeColor != value) {
                    this.disabledForeColor = value;
                    this.OnDisabledForeColorChanged();
                }
            }
        }
        [Description("Gets or sets background color used in the disabled state.")]
        public virtual Color DisabledBackColor {
            get {
                return this.disabledBackColor;
            }
            set {
                if (this.disabledBackColor != value) {
                    this.disabledBackColor = value;
                    this.OnDisabledBackColorChanged();
                }
            }
        }
        [DefaultValue(false), Description("Indicates whether edit control should draw columns indentation marks.")]
        public virtual bool DrawColumnsIndent {
            get {
                return this.drawColumnsIndent;
            }
            set {
                if (this.drawColumnsIndent != value) {
                    this.drawColumnsIndent = value;
                    this.OnDrawColumnsIndentChanged();
                }
            }
        }
        [Description("Specifies columns indentation color.")]
        public virtual Color ColumnsIndentForeColor {
            get {
                return this.columnsIndentForeColor;
            }
            set {
                if (this.columnsIndentForeColor != value) {
                    this.columnsIndentForeColor = value;
                    this.OnColumnsIndentForeColorChanged();
                }
            }
        }
        [DefaultValue(true), Description("Gets or sets a boolean value that indicates whether Edit control should display hint over each syntax error in it's content.")]
        public virtual bool SyntaxErrorsHints {
            get {
                return this.syntaxErrorsHints;
            }
            set {
                if (this.syntaxErrorsHints != value) {
                    this.syntaxErrorsHints = value;
                    this.OnSyntaxErrorsHintsChanged();
                }
            }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual bool NeedPaint {
            get {
                return this.needPaint;
            }
            set {
                this.needPaint = value;
            }
        }
        private void InitSyntaxErrors() {
            if (this.syntaxErrors == null) {
                this.syntaxErrors = new Hashtable();
            }
            this.syntaxErrors.Clear();
            var source = this.owner.Source;
            foreach (SyntaxError current in source.SyntaxErrors) {
                if (current.Position.X >= source.Lines.GetLength(current.Position.Y)) {
                    this.syntaxErrors[current.Position.Y] = current.ErrorType;
                }
            }
        }
        private bool IsWhitespaceOnly(string s) {
            int length = s.Length;
            bool result;
            if (length != 0) {
                for (int i = 0; i < length; i++) {
                    if (!this.displayLines.IsWhitespace(s, i)) {
                        result = false;
                        return result;
                    }
                }
            }
            result = true;
            return result;
        }
        private int IndexOfNonWhitespaceChar(string s) {
            int length = s.Length;
            int result;
            for (int i = 0; i < length; i++) {
                if (!this.displayLines.IsWhitespace(s, i)) {
                    result = i;
                    return result;
                }
            }
            result = -1;
            return result;
        }
        private void DrawHighlighter(int left, int top, int right, int bottom, StringItemInfo style, int pos, int line, string s, bool inSelection, bool afterText) {
            if (this.lineSeparator.LineColor != Color.Empty) {
                this.painter.ForeColor = this.lineSeparator.LineColor;
                Rectangle rect = new Rectangle(left, top, right - left, bottom - top);
                this.painter.DrawLine(left, top, right, top);
                this.painter.DrawLine(left, bottom, right, bottom);

            }
        }
        private void DrawLineStyle(int left, int top, int right, int bottom, StringItemInfo style, int pos, int line, string s, bool inSelection, bool afterText, EditLineStyle lineStyle) {
            if (lineStyle.PenColor != Color.Empty) {
                this.painter.ForeColor = lineStyle.PenColor;
                Rectangle rect = new Rectangle(left, top, right - left, bottom - top);
                this.painter.DrawLine(left, top, right, top);
                this.painter.DrawLine(left, bottom, right, bottom);
            }
        }
        private void FillRectangleAfterLineEnd(int x, int y, int w, int h, int line, bool inSelection, bool highlight, EditLineStyle lineStyle) {
            Rectangle rect = new Rectangle(x, y, w, h);
            this.painter.FillRectangle(x, y, w, h);
            StringItemInfo style = default(StringItemInfo);
            style.Data = 255;
            if (lineStyle != null && lineStyle.PenColor != Color.Empty && (lineStyle.Options & LineStyleOptions.BeyondEol) != LineStyleOptions.None) {
                this.DrawLineStyle(x, y - 1, x + w, y + h - 1, style, 0, line, string.Empty, inSelection, true, lineStyle);
            }
            if (highlight) {
                this.DrawHighlighter(x, y - 1, x + w, y + h - 1, style, 0, line, string.Empty, inSelection, true);
            }
        }

        private void DrawAfterLineEnd(int w, Point position, EditLineStyle lineStyle, bool selLine, bool emptySel, int offset, int left, int right, int index, bool highlight, bool readOnly, bool drawColumnIndent, int start, int firstIndentChar, bool inHighlightOutlineArea) {
            if (w > position.X) {
                Color color = (lineStyle != null && (lineStyle.Options & LineStyleOptions.BeyondEol) != LineStyleOptions.None) ? lineStyle.GetBackColor(this.GetBackColor(readOnly)) : ((highlight && this.lineSeparator.HighlightBackColor != Color.Empty) ? this.lineSeparator.HighlightBackColor : this.GetBackColor(readOnly));
                bool flag = (selLine && (SelectionOptions.SelectBeyondEol & this.selection.Options) != SelectionOptions.None) || this.selection.SelectionType == SelectionType.Block || emptySel;
                bool flag2 = (this.owner.Transparent || this.inPrinting) && !highlight && lineStyle == null;
                int lineHeight = this.painter.lineHeight;
                if (flag) {
                    if (emptySel) {
                        right = left + 1;
                    }
                    int displayWidth = this.gutter.DisplayWidth;
                    int num = this.MeasureLine(index, 0, left);
                    if (num != int.MaxValue) {
                        num -= offset - displayWidth;
                    }
                    int num2 = this.MeasureLine(index, 0, right);
                    if (num2 != int.MaxValue) {
                        num2 -= offset - displayWidth;
                    } else {
                        num2 = w;
                    }
                    if (num2 > position.X) {
                        num = Math.Max(num, position.X);
                        if (num > position.X && !flag2) {
                            this.painter.BackColor = color;
                            this.FillRectangleAfterLineEnd(position.X, position.Y, num - position.X, lineHeight, index, false, highlight, lineStyle);
                        }
                        this.painter.BackColor = this.selBackColor;
                        this.FillRectangleAfterLineEnd(num, position.Y, num2 - num, lineHeight, index, true, highlight, lineStyle);
                        if (num2 < w && !flag2) {
                            this.painter.BackColor = (inHighlightOutlineArea ? EditConsts.DefaultHighlightOutlineAreaColor : (this.inHighlightBlockArea ? EditConsts.DefaultHighlightBlockAreaColor : color));
                            this.FillRectangleAfterLineEnd(num2, position.Y, w - num2, lineHeight, index, false, highlight, lineStyle);
                        }
                        return;
                    }
                }
                if (!flag2) {
                    this.painter.BackColor = (inHighlightOutlineArea ? EditConsts.DefaultHighlightOutlineAreaColor : (this.inHighlightBlockArea ? EditConsts.DefaultHighlightBlockAreaColor : color));
                    this.FillRectangleAfterLineEnd(position.X, position.Y, w - position.X, this.painter.lineHeight, index, false, highlight, lineStyle);
                }
                if (drawColumnIndent) {
                    for (int i = 1; i <= firstIndentChar; i++) {
                        if (i == this.owner.Lines.GetTabStop(i - 1)) {
                            if (!selLine || i < left || i > right) {
                                int x = this.painter.CharWidth(' ', i) + start;
                                this.DrawColumnIndent(x, position.Y, position.Y + lineHeight);
                            }
                        }
                    }
                }
            }
        }
        private void DrawSelectionBorder(Rectangle rect, Border3DSide sides, Color borderColor) {
            if (borderColor != Color.Empty && sides != (Border3DSide)0 && rect.Width >= 0) {
                Color foreColor = this.painter.ForeColor;
                this.painter.ForeColor = borderColor;
                if ((sides & Border3DSide.Left) != (Border3DSide)0) {
                    this.painter.DrawLine(rect.X, rect.Y, rect.X, rect.Y + rect.Height);
                }
                if ((sides & Border3DSide.Right) != (Border3DSide)0) {
                    this.painter.DrawLine(rect.X + rect.Width - 1, rect.Y, rect.X + rect.Width - 1, rect.Y + rect.Height);
                }
                if ((sides & Border3DSide.Top) != (Border3DSide)0) {
                    this.painter.DrawLine(rect.X, rect.Y, rect.X + rect.Width, rect.Y);
                }
                if ((sides & Border3DSide.Bottom) != (Border3DSide)0) {
                    this.painter.DrawLine(rect.X, rect.Y + rect.Height - 1, rect.X + rect.Width, rect.Y + rect.Height - 1);
                }
                this.painter.ForeColor = foreColor;
            }
        }
        private int GetSelectionBorder(int pos, int index) {
            if (pos == int.MaxValue) {
                if ((SelectionOptions.SelectBeyondEol & this.owner.Selection.Options) != SelectionOptions.None || this.selection.SelectionType == SelectionType.Block) {
                    pos = this.owner.ClientRect.Width;
                } else {
                    pos = this.owner.DisplayToScreen(this.displayLines[index].Length, index).X;
                }
            } else {
                pos = this.owner.DisplayToScreen(pos, index).X;
            }
            return pos;
        }
        private bool GetSelectionBorder(int index, out int left, out int right) {
            left = 0;
            right = 0;
            bool result;
            if (index >= 0 && index < this.displayLines.DisplayCount && this.selection.GetSelectionForLine(index, out left, out right)) {
                if (right > left) {
                    left = this.GetSelectionBorder(left, index);
                    right = this.GetSelectionBorder(right, index);
                    result = (right > left);
                    return result;
                }
            }
            result = false;
            return result;
        }
        private void DrawSelectionBorder(int left, int right, int top, int index) {
            int selectionBorder = this.GetSelectionBorder(left, index);
            int selectionBorder2 = this.GetSelectionBorder(right, index);
            if (selectionBorder2 > selectionBorder) {
                this.DrawSelectionBorder(new Rectangle(selectionBorder, top, selectionBorder2 - selectionBorder, this.painter.lineHeight), Border3DSide.Left | Border3DSide.Right, this.selection.owner.selectionBorderColor);
                if (this.GetSelectionBorder(index - 1, out left, out right)) {
                    if (left > selectionBorder) {
                        left = Math.Min(left, selectionBorder2);
                        this.DrawSelectionBorder(new Rectangle(selectionBorder, top, Math.Min(left, right) - selectionBorder, this.painter.lineHeight), Border3DSide.Top, this.selection.owner.selectionBorderColor);
                    }
                    if (right < selectionBorder2) {
                        this.DrawSelectionBorder(new Rectangle(right, top, selectionBorder2 - right, this.painter.lineHeight), Border3DSide.Top, this.selection.owner.selectionBorderColor);
                    }
                } else {
                    this.DrawSelectionBorder(new Rectangle(selectionBorder, top, selectionBorder2 - selectionBorder, this.painter.lineHeight), Border3DSide.Top, this.selection.owner.selectionBorderColor);
                }
                if (this.GetSelectionBorder(index + 1, out left, out right)) {
                    if (left > selectionBorder) {
                        this.DrawSelectionBorder(new Rectangle(selectionBorder, top, selectionBorder - left, this.painter.lineHeight), Border3DSide.Bottom, this.selection.owner.selectionBorderColor);
                    }
                    if (right < selectionBorder2) {
                        right = Math.Max(right, selectionBorder);
                        this.DrawSelectionBorder(new Rectangle(right, top, selectionBorder2 - right, this.painter.lineHeight), Border3DSide.Bottom, this.selection.owner.selectionBorderColor);
                    }
                } else {
                    this.DrawSelectionBorder(new Rectangle(selectionBorder, top, selectionBorder2 - selectionBorder, this.painter.lineHeight), Border3DSide.Bottom, this.selection.owner.selectionBorderColor);
                }
            }
        }
        private void DrawCodeSnippets(Painter painter, Point location, int startLine, int endLine) {
            var source = this.owner.Source;
            if (source.CurrentSnippet != null && source.CodeSnippets.IsFirstSnippet(source.CurrentSnippet)) {
                foreach (CodeSnippetRange current in source.CodeSnippets) {
                    if (current.ID == source.CurrentSnippet.ID && (current.EndPoint.Y != current.StartPoint.Y || current.EndPoint.X != current.StartPoint.X) && current.StartPoint.Y <= endLine && current.EndPoint.Y >= startLine) {
                        Point location2 = this.owner.TextToScreen(current.StartPoint);
                        Point point = this.owner.TextToScreen(current.EndPoint);
                        Rectangle rectangle = new Rectangle(location2, new Size(point.X - location2.X, point.Y - location2.Y + painter.FontHeight));
                        Color color = (this.snippetStyle != null) ? this.snippetStyle.ForeColor : SyntaxConsts.DefaultCodeSnippetForeColor;
                        if (current == source.CurrentSnippet) {
                            Color backColor = painter.BackColor;
                            painter.BackColor = color;
                            painter.DrawRectangle(rectangle.Left, rectangle.Top, rectangle.Width - 1, rectangle.Height - 1);
                            painter.BackColor = backColor;
                        } else {
                            painter.DrawFocusRect(rectangle.Left, rectangle.Top, rectangle.Width - 1, rectangle.Height - 1, color);
                        }
                    }
                }
            }
        }
        private void DrawColumnIndent(int x, int top, int bottom) {
            Color foreColor = this.painter.ForeColor;
            this.painter.ForeColor = this.ColumnsIndentForeColor;
            this.painter.DrawLine(x, top, x, bottom);
            this.painter.ForeColor = foreColor;
        }
        private int DrawTextFragment(Point position, string s, StringItemInfo style, EditLineStyle lineStyle, bool inSelection, int line, int startChar, int endChar, bool specialSymbol, bool highlight, bool readOnly, bool columnIndent, bool inHighlightOutlineArea, bool InHighlightBlockArea) {
            return this.DrawTextFragment(position, s, style, lineStyle, inSelection, line, startChar, endChar, specialSymbol, highlight, readOnly, columnIndent, inHighlightOutlineArea, InHighlightBlockArea, false);
        }
        private int DrawTextFragment(Point position, string s, StringItemInfo style, EditLineStyle lineStyle, bool inSelection, int line, int startChar, int endChar, bool specialSymbol, bool highlight, bool readOnly, bool columnIndent, bool inHighlightOutlineArea, bool InHighlightBlockArea, bool emptySel) {
            TextStyle textStyle = TextStyle.None;
            LexStyle lexStyle = this.GetLexStyle(style, ref textStyle);
            if (this.DisableSyntaxPaint) {
                lexStyle = null;
            }
            string text = (startChar == 0 && endChar == -1) ? s : ((endChar >= s.Length) ? string.Empty : s.Substring(startChar, endChar - startChar + 1));
            if (lexStyle != null) {
                this.painter.fontStyle = this.GetFontStyle(lexStyle.FontStyle, textStyle);
                if (inSelection) {
                    this.painter.TextColor = this.GetSelectionForeColor(this.GetFontColor(lexStyle.ForeColor, textStyle));
                    this.painter.BackColor = this.selBackColor;
                } else {
                    if (highlight && this.lineSeparator.HighlightForeColor != Color.Empty) {
                        this.painter.TextColor = this.lineSeparator.HighlightForeColor;
                    } else {
                        this.painter.TextColor = this.GetFontColor(lexStyle.ForeColor, textStyle);
                    }
                    if (highlight && this.lineSeparator.HighlightBackColor != Color.Empty) {
                        this.painter.BackColor = this.lineSeparator.HighlightBackColor;
                    } else {
                        this.painter.BackColor = this.GetBackColor((lexStyle.BackColor != Color.Empty) ? lexStyle.BackColor : this.GetBackColor(readOnly), textStyle);
                    }
                    if (inHighlightOutlineArea) {
                        this.painter.BackColor = EditConsts.DefaultHighlightOutlineAreaColor;
                    } else {
                        if (this.inHighlightBlockArea) {
                            this.painter.BackColor = EditConsts.DefaultHighlightBlockAreaColor;
                        }
                    }
                }
            } else {
                this.painter.fontStyle = this.GetFontStyle(this.owner.Font.Style, textStyle);
                if (inSelection) {
                    this.painter.TextColor = this.selForeColor;
                    this.painter.BackColor = this.selBackColor;
                } else {
                    if (highlight && this.lineSeparator.HighlightForeColor != Color.Empty) {
                        this.painter.TextColor = this.lineSeparator.HighlightForeColor;
                    } else {
                        this.painter.TextColor = this.GetFontColor(this.owner.ForeColor, textStyle);
                    }
                    if (highlight && this.lineSeparator.HighlightBackColor != Color.Empty) {
                        this.painter.BackColor = this.lineSeparator.HighlightBackColor;
                    } else {
                        this.painter.BackColor = this.GetBackColor(this.GetBackColor(readOnly), textStyle);
                    }
                    if (inHighlightOutlineArea) {
                        this.painter.BackColor = EditConsts.DefaultHighlightOutlineAreaColor;
                    } else {
                        if (this.inHighlightBlockArea) {
                            this.painter.BackColor = EditConsts.DefaultHighlightBlockAreaColor;
                        }
                    }
                }
            }
            Color textColor = Color.Empty;
            if (!inSelection && lineStyle != null && (lexStyle == null || lexStyle.BackColor == Color.Empty) && (byte) (textStyle & TextStyle.OutlineSection) == 0 && (byte) (textStyle & TextStyle.CodeSnippet) == 0) {
                this.painter.BackColor = lineStyle.GetBackColor(this.painter.BackColor);
                textColor = lineStyle.GetForeColor(this.painter.TextColor);
                this.painter.TextColor = textColor;
            }
            int num = this.painter.measureString(text);
            Rectangle rect = new Rectangle(position.X, position.Y, num, this.painter.lineHeight);
            Rectangle rect2 = new Rectangle(position.X, position.Y, num, this.painter.FontHeight);
            int space = -1;
            if (!specialSymbol && this.whiteSpace.Visible) {
                if (this.whiteSpace.SpaceSymbol != '\0' && (byte) (textStyle & TextStyle.WhiteSpace) != 0) {
                    space = this.painter.measureString(text[0] );
                    text = text.Replace('\u3000', this.whiteSpace.LongSpaceSymbol).Replace(' ', this.whiteSpace.SpaceSymbol);
                } else {
                    if (this.whiteSpace.TabSymbol != '\0' && (byte) (textStyle & TextStyle.Tabulation) != 0) {
                        space = this.painter.measureString(' ' );
                        text = new string(this.whiteSpace.TabSymbol, text.Length);
                    }
                }
            }
            bool flag = (this.owner.Transparent || this.inPrinting) && !inSelection && !highlight && lineStyle == null;
            if (flag) {
                this.painter.Opaque = false;
            }
            this.painter.TextOut(text, -1, rect, rect.Left, rect.Top, !this.owner.Transparent && !this.inPrinting, !this.owner.Transparent && !this.inPrinting, space);
            if (emptySel) {
                Color backColor = this.painter.BackColor;
                try {
                    this.painter.BackColor = this.selBackColor;
                    this.painter.FillRectangle(new Rectangle(position.X, position.Y, 1, this.painter.lineHeight));
                } finally {
                    this.painter.BackColor = backColor;
                }
            }
             
            if (flag) {
                this.painter.Opaque = true;
            }
            if (lineStyle != null && lineStyle.PenColor != Color.Empty) {
                this.DrawLineStyle(rect.Left, rect.Top - 1, rect.Right, rect.Bottom - 1, style, startChar, line, text, inSelection, false, lineStyle);
            } else {
                if (highlight) {
                    this.DrawHighlighter(rect2.Left, rect2.Top - 1, rect2.Right, rect2.Bottom + this.owner.Painter.LineSpace - 1, style, startChar, line, text, inSelection, false);
                }
            }
            if ((byte) (textStyle & TextStyle.OutlineSection) != 0) {
                Color color = this.painter.ForeColor;
                Color backColor2 = this.painter.BackColor;
                this.painter.BackColor = this.outlining.OutlineColor;
                this.painter.ForeColor = this.outlining.OutlineColor;
                if (this.outlining.UseRoundRect) {
                    this.painter.DrawRoundRectangle(rect2.Left, rect2.Top, rect2.Right - 1, rect2.Bottom - 1, 2, 2);
                } else {
                    this.painter.DrawRectangle(rect2.Left, rect2.Top, rect2.Width - 1, rect2.Height - 1);
                }

                this.painter.ForeColor = color;
                this.painter.BackColor = backColor2;
            }
            if (columnIndent) {
                this.DrawColumnIndent(rect.Left, rect.Top, rect.Bottom);
            }
            if (this.braces.UseRoundRect && (byte) (textStyle & TextStyle.Brace) != 0 && (byte) (textStyle & TextStyle.Tabulation) == 0 && (byte) (textStyle & TextStyle.WhiteSpace) == 0 && !this.IsWhitespaceOnly(text)) {
                Color color = this.painter.BackColor;
                this.painter.BackColor = this.braces.BackColor;
                this.painter.DrawRectangle(rect2.Left, rect2.Top, rect2.Width - 1, rect2.Height - 1);

                this.painter.BackColor = color;
            }
            if ((byte) (textStyle & TextStyle.WaveLine) != 0) {
                this.painter.DrawWave(rect2, this.GetWaveColor(textStyle ^ TextStyle.WaveLine));
            }
            return num;
        }

        protected virtual Color GetWaveColor(TextStyle style) {
            Color result;
            if (style != TextStyle.WaveLine) {
                result = this.spelling.SpellColor;
            } else {
                result = ((this.errorStyle != null) ? this.errorStyle.ForeColor : SyntaxConsts.DefaultSyntaxErrorsForeColor);
            }
            return result;
        }
   
        protected virtual bool EqualStyles(StringItemInfo style1, StringItemInfo style2, int index1, int index2, string s, bool useColors) {
            bool result;
            if (this.whiteSpace.Visible) {
                TextStyle textStyle = style1.TextStyle;
                TextStyle textStyle2 = style2.TextStyle;
                if ((byte)(textStyle & TextStyle.WhiteSpace) != 0 && (byte)(textStyle2 & TextStyle.WhiteSpace) != 0 && s[index1] != s[index2]) {
                    result = false;
                    return result;
                }
            }
            result = base.EqualStyles(style1, style2, useColors);
            return result;
        }
        private void DrawLine(int index, int realLine, string line, StringItemInfo[] colorData, Point position, Rectangle clipRect) {
            this.inHighlightOutlineArea = (this.hightlightOutlineRange != null && realLine >= this.hightlightOutlineRange.StartPoint.Y && realLine <= this.hightlightOutlineRange.EndPoint.Y);
            this.inHighlightBlockArea = (this.hightlightBlockRange != null && realLine >= this.hightlightBlockRange.StartPoint.Y && realLine <= this.hightlightBlockRange.EndPoint.Y);
            int right = clipRect.Right;
            int num = 0;
            int num2 = 0;
            int x = position.X;
            bool inSelection = (this.drawSelection & !this.selection.IsEmpty) && this.selection.GetSelectionForLine(index, out num, out num2);
            bool flag2 = false;
            bool columnIndent = false;
            bool readOnly = false;
            IList<ILineStyle> list = this.internalLineStyles;
            this.owner.Source.LineStyles.GetLineStyles(realLine, list);
            EditLineStyle editLineStyle = this.GetLineStyleAt(index, 0, list);
            bool checkColumnIndent = editLineStyle == null && this.DrawColumnsIndent && this.ColumnsIndentForeColor != Color.Empty;
            int length = line.Length;
            bool highlight = this.lineSeparator.NeedHighlightDisplayLine(index);
            int num3 = this.gutter.DisplayWidth - position.X;
            bool flag5 = false;
            if (this.whiteSpace.Visible || this.syntaxErrors.Count > 0 || checkColumnIndent) {
                flag5 = (this.owner.WordWrap && this.displayLines.DisplayPointToPoint(0, index + 1).X > 0 && index < this.displayLines.DisplayCount - 1);
            }
            if (flag5) {
                checkColumnIndent = false;
            }
            int num4 = 0;
            if (checkColumnIndent) {
                num4 = this.IndexOfNonWhitespaceChar(line);
                if (num4 < 0) {
                    for (int i = index - 1; i >= 0; i--) {
                        StringItemInfo[] array = null;
                        string text = string.Empty;
                        this.GetString(i, ref text, ref array);
                        num4 = this.IndexOfNonWhitespaceChar(text);
                        if (num4 >= 0) {
                            break;
                        }
                    }
                }
            }
            if (line != string.Empty) {
                StringItemInfo stringItemInfo = colorData[0];
                StringItemInfo stringItemInfo2 = stringItemInfo;
                int num5 = 0;
                flag2 = (inSelection && num == 0 && num2 > 0);
                for (int i = 1; i < length; i++) {
                    stringItemInfo = colorData[i];
                    bool flag6 = inSelection && num <= i && num2 > i;
                    EditLineStyle lineStyleAt = this.GetLineStyleAt(index, i, list);
                    bool oldcolumnIndent = checkColumnIndent && !flag2 && i < num4 && i == this.owner.Lines.GetTabStop(i - 1);
                    bool flag8 = inSelection && num == num2 && num >= num5 && num <= i;
                    bool emptySel = inSelection && num == num2 && num >= num5 && num < i;
                    if (i - num5 > EditConsts.MaxPaintChar || flag6 != flag2 || columnIndent != oldcolumnIndent || !this.EqualStyles(stringItemInfo2, stringItemInfo, num5, i, line, true) || !this.EqualLineStyles(lineStyleAt, editLineStyle, stringItemInfo, stringItemInfo2) || flag8) {
                        position.X += this.DrawTextFragment(position, line, stringItemInfo2, editLineStyle, flag2, index, num5, i - 1, false, highlight, readOnly, columnIndent, this.inHighlightOutlineArea, this.inHighlightBlockArea, emptySel);
                        if (position.X >= right) {
                            break;
                        }
                        stringItemInfo2 = stringItemInfo;
                        num5 = i;
                        editLineStyle = lineStyleAt;
                        flag2 = flag6;
                        columnIndent = oldcolumnIndent;
                    }
                }
                if (num5 < length && position.X < right) {
                    position.X += this.DrawTextFragment(position, line, stringItemInfo2, editLineStyle, flag2, index, num5, length - 1, false, highlight, readOnly, columnIndent, this.inHighlightOutlineArea, this.inHighlightBlockArea);
                }
            }
            if (position.X < right) {
                if (this.whiteSpace.Visible) {
                    string text = string.Empty;
                    if (flag5 && this.whiteSpace.WordWrapSymbol != '\0') {
                        text = this.whiteSpace.WordWrapString;
                    } else {
                        if (!flag5) {
                            bool flag9 = index == this.displayLines.DisplayCount - 1;
                            if (flag9 && this.whiteSpace.EofSymbol != '\0') {
                                text = this.whiteSpace.EofString;
                            } else {
                                if (!flag9 && this.whiteSpace.EolSymbol != '\0') {
                                    text = this.whiteSpace.EolString;
                                }
                            }
                        }
                    }
                    if (text != string.Empty) {
                        flag2 = (inSelection && ((SelectionOptions.SelectBeyondEol & this.owner.Selection.Options) != SelectionOptions.None || this.selection.SelectionType == SelectionType.Block) && (inSelection && num <= length) && num2 > length);
                        StringItemInfo style = default(StringItemInfo);
                        style.TextStyle = TextStyle.WhiteSpace;
                        position.X += this.DrawTextFragment(position, text, style, editLineStyle, flag2, index, 0, 0, true, highlight, readOnly, columnIndent, this.inHighlightOutlineArea, this.inHighlightBlockArea);
                    }
                }
                this.DrawAfterLineEnd(right, position, editLineStyle, inSelection, inSelection && line == string.Empty && num == 0 && num2 == int.MaxValue && (SelectionOptions.SelectBeyondEol & this.selection.Options) == SelectionOptions.None, num3, num, num2, index, highlight, readOnly, checkColumnIndent, x, this.IsWhitespaceOnly(line) ? num4 : 0, this.inHighlightOutlineArea);
                if (inSelection && num == num2 && num >= length) {
                    int displayWidth = this.gutter.DisplayWidth;
                    int num6 = this.MeasureLine(index, 0, num2);
                    if (num6 != int.MaxValue) {
                        num6 -= num3 - displayWidth;
                        this.DrawTextFragment(new Point(num6, position.Y), line, default(StringItemInfo), editLineStyle, false, index, length, length, false, highlight, readOnly, columnIndent, this.inHighlightOutlineArea, true);
                    }
                }
                if (inSelection && (this.selection.Options & SelectionOptions.DrawBorder) != SelectionOptions.None && this.selection.owner.selectionBorderColor != Color.Empty) {
                    this.DrawSelectionBorder(num, num2, position.Y, index);
                }
                if (right > position.X && !flag5) {
                    object obj = this.syntaxErrors[realLine];
                    if (obj != null) {
                        this.DrawErrorAfterLineEnd(position, realLine, (!flag2) ? editLineStyle : null, (SyntaxErrorType)obj);
                    }
                }
            }
        }
        private void DrawErrorAfterLineEnd(Point position, int line, EditLineStyle lineStyle, SyntaxErrorType errorType) {
            Rectangle rect = new Rectangle(position.X, position.Y, this.painter.FontWidth, this.painter.FontHeight);

            Color color = (lineStyle != null) ? lineStyle.GetForeColor(this.painter.TextColor) : Color.Empty;
            Color color2 = SyntaxConsts.DefaultSyntaxErrorsForeColor;
            switch (errorType) {
                case SyntaxErrorType.Error:
                    if (this.errorStyle != null) {
                        color2 = this.errorStyle.ForeColor;
                    }
                    break;
                case SyntaxErrorType.Warning:
                    color2 = this.spelling.SpellColor;
                    break;
            }
            this.painter.DrawWave(rect, (color != Color.Empty) ? color : color2);

        }
        private bool DrawEndLine(Rectangle rect, Point position, int endLine, bool readOnly, EditLineStyle lineStyle) {
            bool result;
            if (this.whiteSpace.Visible && this.whiteSpace.EofSymbol != '\0') {
                StringItemInfo style = default(StringItemInfo);
                style.TextStyle = TextStyle.WhiteSpace;
                int right = rect.Right;
                int count = this.owner.Lines.Count;
                bool highlight = this.lineSeparator.NeedHighlightLine(count);
                position.X += this.DrawTextFragment(position, this.whiteSpace.EofString, style, lineStyle, false, endLine, 0, 0, true, highlight, readOnly, false, false, false);
                this.DrawAfterLineEnd(right, position, lineStyle, false, false, 0, 0, 0, count, highlight, readOnly, false, 0, 0, false);
                result = true;
            } else {
                result = false;
            }
            return result;
        }
        protected EditLineStyle GetLineStyleAt(int line, int pos, IList<ILineStyle> styles) {
            ILineStyle lineStyle = null;
            EditLineStyle result;
            if (styles.Count > 0) {
                Point point = this.displayLines.DisplayPointToPoint(pos, line);
                foreach (ILineStyle current in styles) {
                    Point point2 = (current.Range != null) ? current.Range.StartPoint : new Point(current.Pos, current.Line);
                    Point point3 = (current.Range != null) ? current.Range.EndPoint : new Point(int.MaxValue, current.Line);
                    if ((point.Y > point2.Y || (point.Y == point2.Y && point.X >= point2.X)) && (point.Y < point3.Y || (point.Y == point3.Y && point.X < point3.X))) {
                        lineStyle = ((lineStyle != null && lineStyle.Priority >= current.Priority) ? lineStyle : current);
                    }
                }
                if (lineStyle != null && lineStyle.Index >= 0 && lineStyle.Index < this.lineStyles.Count) {
                    result = this.lineStyles[lineStyle.Index];
                    return result;
                }
            }
            result = null;
            return result;
        }
        protected Color GetSelectionForeColor(Color color) {
            Color result;
            if ((SelectionOptions.UseColors & this.selection.Options) != SelectionOptions.None) {
                result = color;
            } else {
                result = this.selForeColor;
            }
            return result;
        }
        protected override void GetString(int index, ref string text, ref StringItemInfo[] colorData) {
            this.displayLines.GetStringAndColorData(index, ref text, ref colorData);
        }
        protected override void OnDisableColorPaintChanged() {
            base.OnDisableColorPaintChanged();
            this.owner.Invalidate();
        }
        protected override void OnDisableSyntaxPaintChanged() {
            base.OnDisableSyntaxPaintChanged();
            this.owner.Invalidate();
        }
        protected virtual void OnDrawColumnsIndentChanged() {
            this.owner.Invalidate();
        }
        protected virtual void OnColumnsIndentForeColorChanged() {
            if (this.drawColumnsIndent) {
                this.owner.Invalidate();
            }
        }
        protected virtual void OnReadonlyForeColorChanged() {
            if (this.owner.ReadOnly) {
                this.owner.Invalidate();
            }
        }
        protected virtual void OnReadonlyBackColorChanged() {
            if (this.owner.ReadOnly) {
                this.owner.Invalidate();
            }
        }
        protected virtual void OnDisabledForeColorChanged() {
            if (!this.owner.Enabled) {
                this.owner.Invalidate();
            }
        }
        protected virtual void OnDisabledBackColorChanged() {
            if (!this.owner.Enabled) {
                this.owner.Invalidate();
            }
        }
        protected virtual void OnSyntaxErrorsHintsChanged() {
        }
        public EditSyntaxPaint(Painter painter, CodeEditor owner)
            : base(painter, (Control)owner) {
            this.owner = owner;
            this.displayLines = owner.DisplayLines;
            this.whiteSpace = owner.WhiteSpace;
            this.selection = owner.Selection;
            this.lineStyles = owner.LineStyles;
            this.lineSeparator = owner.LineSeparator;
            this.gutter = owner.Gutter;
            this.braces = owner.Braces;
            this.outlining = owner.Outlining;
            this.spelling = owner.Spelling;
            this.hyperText = owner.HyperText;
            this.internalLineStyles = new List<ILineStyle>();
        }
        protected override bool NeedParse() {
            return this.owner.Source.NeedParse();
        }
        public override FontStyle GetFontStyle(FontStyle fontStyle, TextStyle textStyle) {
            FontStyle result;
            if ((byte)(textStyle & TextStyle.HyperText) != 0) {
                result = (fontStyle | this.hyperText.UrlStyle);
            } else {
                if ((byte)(textStyle & TextStyle.Brace) != 0 && !this.braces.UseRoundRect) {
                    result = (fontStyle | this.braces.FontStyle);
                } else {
                    result = fontStyle;
                }
            }
            return result;
        }
        public override Color GetFontColor(Color color, TextStyle textStyle) {
            Color result;
            if (this.DisableColorPaint) {
                result = this.owner.ForeColor;
            } else {
                if ((byte)(textStyle & TextStyle.HyperText) != 0) {
                    Color color2 = this.hyperText.UrlColor;
                    if (color2 != Color.Empty) {
                        result = color2;
                        return result;
                    }
                }
                if ((byte)(textStyle & TextStyle.WhiteSpace) != 0 || (byte)(textStyle & TextStyle.Tabulation) != 0) {
                    Color color2 = this.whiteSpace.SymbolColor;
                    if (color2 != Color.Empty) {
                        result = color2;
                        return result;
                    }
                }
                if ((byte)(textStyle & TextStyle.OutlineSection) != 0) {
                    Color color2 = this.outlining.OutlineColor;
                    if (color2 != Color.Empty) {
                        result = color2;
                        return result;
                    }
                }
                if ((byte)(textStyle & TextStyle.Brace) != 0 && !this.braces.UseRoundRect) {
                    Color color2 = this.braces.ForeColor;
                    if (color2 != Color.Empty) {
                        result = color2;
                        return result;
                    }
                }
                result = color;
            }
            return result;
        }
        public override Color GetBackColor(Color color, TextStyle state) {
            Color result;
            if ((byte)(state & TextStyle.CodeSnippet) != 0) {
                Color color2 = (this.snippetStyle != null) ? this.snippetStyle.BackColor : SyntaxConsts.DefaultCodeSnippetBackColor;
                if (color2 != Color.Empty) {
                    result = color2;
                    return result;
                }
            }
            if ((byte)(state & TextStyle.Brace) != 0 && !this.braces.UseRoundRect) {
                Color color2 = this.braces.BackColor;
                if (color2 != Color.Empty) {
                    result = color2;
                    return result;
                }
            }
            result = ((color != Color.Empty) ? color : this.painter.BackColor);
            return result;
        }
        public override void DrawLine(int index, Point position, Rectangle clipRect) {
            string empty = string.Empty;
            StringItemInfo[] colorData = null;
            int stringAndColorData = this.displayLines.GetStringAndColorData(index, ref empty, ref colorData);
            this.DrawLine(index, stringAndColorData, empty, colorData, position, clipRect);
        }
        public virtual void DrawLine(int index, Point position, Rectangle clipRect, out int realLine) {
            string empty = string.Empty;
            StringItemInfo[] colorData = null;
            realLine = this.displayLines.GetStringAndColorData(index, ref empty, ref colorData);
            this.DrawLine(index, realLine, empty, colorData, position, clipRect);
        }
        public override void PaintSyntax(Painter painter, int startLine, int endLine, Point position, Rectangle rect, Point location, bool specialPaint) {
            this.InitSyntaxErrors();
            this.internalLineStyles.Clear();
            if (this.owner.Focused || this.owner.IsCodeCompletionWindowFocused) {
                this.selBackColor = this.selection.owner.selectionBackColor;
                this.selForeColor = this.selection.ForeColor;
                this.drawSelection = true;
            } else {
                this.drawSelection = ((SelectionOptions.HideSelection & this.selection.Options) == SelectionOptions.None);
                this.selBackColor = this.selection.owner.inactivedSelectionBackColor;
                this.selForeColor = this.selection.InActiveForeColor;
            }
            this.errorStyle = ((this.Lexer != null) ? this.Lexer.Scheme.Styles.FindLexStyle(StringConsts.SyntaxErrorsInternalName) : null);
            this.snippetStyle = ((this.Lexer != null) ? this.Lexer.Scheme.Styles.FindLexStyle(StringConsts.CodeSnippetsInternalName) : null);
            int lineHeight = painter.lineHeight;
            int index = 0;
            this.hightlightOutlineRange = this.owner.ActiveOutlineRange;
            this.hightlightBlockRange = this.owner.CurrentBlockRange;
            int i = startLine;
            while (i < endLine) {
                this.DrawLine(i, position, rect, out index);
                position.Y += lineHeight;
                if (position.Y > rect.Bottom || (position.Y == rect.Bottom && specialPaint)) {
                    break;
                }
                goto IL_306;
            IL_306:
                i++;
                continue;
            }
            this.DrawCodeSnippets(painter, location, startLine, endLine);
            if (position.Y < rect.Bottom && endLine == startLine && this.DrawEndLine(rect, position, endLine, false, null)) {
                position.Y += lineHeight;
            }
            if (position.Y < rect.Bottom) {
                painter.BackColor = this.GetBackColor(false);
                Rectangle rect2 = new Rectangle(0, position.Y, rect.Right, rect.Bottom - position.Y);
                if (!this.owner.Transparent && !this.inPrinting) {

                    painter.FillRectangle(rect2);

                }
            }
        }
        public override void PaintLineBookMarks(Painter painter, Rectangle rect) {
            BookMarks bookMarks = this.owner.Source.BookMarks;
            for (int i = 0; i < bookMarks.Count; i++) {
                this.PaintLineBookMark(painter, bookMarks[i], rect);
            }
        }
        public virtual void PaintLineBookMark(Painter painter, IBookMark bookMark, Rectangle rect) {
            if (!this.outlining.AllowOutlining || this.outlining.IsVisible(new Point(bookMark.Pos, bookMark.Line))) {
                int num = 3;
                Point point = this.displayLines.PointToDisplayPoint(bookMark.Position.X, bookMark.Position.Y, false);
                Point point2 = this.owner.DisplayToScreen(point.X, point.Y);
                point2.Y += painter.FontHeight - 1;
                if (point2.X + num >= this.gutter.DisplayWidth && point2.X - num < rect.Width && point2.Y > 0 && point2.Y - num < rect.Height) {
                    Point[] points = new Point[] {
                        new Point(point2.X - num, point2.Y),
                        new Point(point2.X + num, point2.Y),
                        new Point(point2.X, point2.Y - num)
                    };
                    Rectangle rect2 = new Rectangle(point2.X - num, point2.Y - num, num*2, num*2);
                    painter.DrawPolygon(points, this.gutter.LineBookmarksColor);
                }
            }
        }
        protected void EnsureLastLineParsed() {
            int num;

            {
                if (this.owner.Scrolling.ScrollByPixels) {
                    num = this.owner.GetLinesInHeight(this.owner.Scrolling.WindowOriginY) + 1;
                } else {
                    num = this.owner.Scrolling.WindowOriginY + this.owner.LinesInHeight + 1;
                }
            }
            num = this.owner.DisplayLines.DisplayPointToPoint(0, num).Y;
            this.owner.Source.ParseToString(num + EditConsts.DefaultParserDelta);
        }
        public virtual Color GetForeColor(bool readOnly) {
            Color result;
            if (!this.owner.Enabled) {
                Color color = this.DisabledForeColor;
                if (color != Color.Empty) {
                    result = color;
                    return result;
                }
            }
            if (this.owner.ReadOnly || readOnly) {
                Color color = this.ReadonlyForeColor;
                if (color != Color.Empty) {
                    result = color;
                    return result;
                }
            }
            result = this.owner.ForeColor;
            return result;
        }
        public virtual Color GetBackColor(bool readOnly) {
            Color result;
            if (!this.owner.Enabled) {
                Color color = this.DisabledBackColor;
                if (color != Color.Empty) {
                    result = color;
                    return result;
                }
            }
            if (this.owner.ReadOnly || readOnly) {
                Color color = this.ReadonlyBackColor;
                if (color != Color.Empty) {
                    result = color;
                    return result;
                }
            }
            result = this.owner.BackColor;
            return result;
        }
        public virtual Region GetRectRegion(SelectionType selectionType, Rectangle rect, bool atTopLeftEnd, bool atBottomRightEnd) {
            Region region = null;
            if (selectionType != SelectionType.None) {
                int lineHeight = this.painter.lineHeight;
                Point position = this.displayLines.PointToDisplayPoint(rect.Left, rect.Top, atTopLeftEnd);
                Point position2 = this.displayLines.PointToDisplayPoint(rect.Right, rect.Bottom, atBottomRightEnd);
                if (selectionType == SelectionType.Block && position.X == position2.X && position.Y != position2.Y) {
                    if (position.X == 0) {
                        position2.Y--;
                    }
                    position.X = 0;
                    position2.X = int.MaxValue;
                }
                position = this.owner.DisplayToScreen(position.X, position.Y);
                position2 = this.owner.DisplayToScreen(position2.X, position2.Y);
                if (position2.X == int.MaxValue) {
                    position2.X = this.owner.ClientRect.Right;
                }
                if (position.Y == position2.Y || selectionType == SelectionType.Block) {
                    int num = Math.Min(position.X, position2.X);
                    int num2 = Math.Max(position.X, position2.X);
                    region = new Region(new Rectangle(num, position.Y, num2 - num + 1, position2.Y - position.Y + lineHeight));
                } else {
                    int num3 = position2.Y - position.Y;
                    {
                        int num = this.gutter.DisplayWidth + this.owner.ClientRect.Left;
                        int num2 = this.owner.ClientRect.Right;
                        region = new Region(new Rectangle(position.X, position.Y, num2 - position.X + 1, lineHeight));
                        region.Union(new Rectangle(num, position.Y + lineHeight, num2 - num + 1, num3 - lineHeight));
                        region.Union(new Rectangle(num, position2.Y, position2.X - num + 1, lineHeight));
                    }
                }
            }
            return region;
        }
        public virtual Region GetRectRegion(Rectangle rect) {
            return this.GetRectRegion(SelectionType.Stream, rect, false, false);
        }
        public virtual void PaintWindow(Painter painter, int startLine, Rectangle rect, Point location, float scaleX, float scaleY, bool specialPaint, bool inPrinting) {
            if (specialPaint) {
                painter.Transform(location.X, location.Y, scaleX, scaleY);
            }
            this.inPrinting = inPrinting;
            try {
                Rectangle clientArea = this.owner.ClientArea;
                Rectangle rectangle = new Rectangle(clientArea.Left, rect.Top, this.gutter.DisplayWidth, rect.Bottom);
                Rectangle rectangle2 = new Rectangle(this.owner.DisplayToScreen(this.owner.EditMargin.Position, startLine, true).X, rect.Top, 1, rect.Height);
                Point position = new Point(rectangle.Width, 0);
                if (this.owner.Scrolling.ScrollByPixels) {
                    position.X -= this.owner.Scrolling.WindowOriginX;
                } else {
                    position.X -= painter.FontWidth*this.owner.Scrolling.WindowOriginX;
                }
                int num = 0;
                if (painter.lineHeight != 0 && !this.owner.Scrolling.ScrollByPixels) {
                    num = (rect.Top - clientArea.Top)/painter.lineHeight;
                    position.Y += num*painter.lineHeight;
                }
                num += startLine;
                bool flag = rectangle.Width > 0 && !Rectangle.Intersect(rect, rectangle).IsEmpty;
                bool flag2 = this.owner.EditMargin.Visible && !this.owner.EditMargin.IsDragging && !Rectangle.Intersect(rect, rectangle2).IsEmpty;

                IntPtr rgn = (!inPrinting) ? painter.SaveClip(this.owner.ClientRectangle) : IntPtr.Zero;
                painter.fontStyle = this.owner.Font.Style;
                if (flag) {
                    rectangle.Offset(-clientArea.Left, -clientArea.Top);
                    this.gutter.Paint(painter, rectangle, startLine);
                    if (!inPrinting) {
                        painter.ExcludeClipRect(rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height);
                    }
                }
                if (flag2) {
                    rectangle2.Offset(-clientArea.Left, -clientArea.Top);
                    this.owner.EditMargin.Paint(painter, rectangle2);
                    if (!inPrinting) {
                        painter.ExcludeClipRect(rectangle2.Left, rectangle2.Top, rectangle2.Width, rectangle2.Height);
                    }
                }
                try {
                    if (specialPaint && !inPrinting) {
                        painter.IntersectClipRect(rect.Left - clientArea.Left, rect.Top - clientArea.Top, rect.Width, rect.Height);
                    }
                    painter.TextColor = this.owner.ForeColor;
                    painter.BackColor = this.GetBackColor(false);
                    if (this.owner.Source.NeedParse()) {
                        this.EnsureLastLineParsed();
                    }
                    this.PaintSyntax(painter, num, this.displayLines.DisplayCount, position, rect, location, specialPaint);
                    if (this.owner.EditMargin.ColumnsVisible) {
                        int[] columnPositions = this.owner.EditMargin.ColumnPositions;
                        for (int i = 0; i < columnPositions.Length; i++) {
                            int x = columnPositions[i];
                            rectangle2 = new Rectangle(this.owner.DisplayToScreen(x, startLine, true).X, rect.Top, 1, rect.Height);
                            if (rectangle2.Left > rect.Right) {
                                break;
                            }

                            rectangle2.Offset(-clientArea.Left, -clientArea.Top);
                            this.owner.EditMargin.PaintColumn(painter, rectangle2);
                        }
                    }
                } finally {
                    if (!inPrinting) {
                        painter.RestoreClip(rgn);
                    }
                }
            } finally {
                this.inPrinting = false;
                if (specialPaint) {
                    painter.EndTransform();
                }
            }
        }
     
    }
}
